import {Component, OnInit, ViewEncapsulation, ViewChild, OnDestroy} from '@angular/core';
import {Subject} from "rxjs/Rx";
import {Subscription} from "rxjs/Subscription";
import { Router} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {CommunityService} from "../community.service";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {SocialCircle} from "@interface/index";



@Component({
  selector: 'all-circle',
  templateUrl: './all-circle.component.html',
  styleUrls: ['./all-circle.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AllCircleComponent implements OnInit, OnDestroy {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public subscription: Subscription = new Subscription();
  public params = {
    page: 1,
    rows: 10
  };
  public circleList: Array<SocialCircle> = [];
  constructor(public router: Router,
              public communityService: CommunityService,
              public toolsService: ToolsService) {
    this.getAllCircles();
  }

  /**
   * @description 获取所有圈子列表
   * @author zhupw1360
   * @date 2017-12-12
   */
  getAllCircles() {
    this.communityService.getAllCircles(this.params)
      .takeUntil(this.ngUnsubscribe).subscribe(res => {
      if (res.result === '0' && res.data && res.data.list.length > 0) {
        this.params.page++;
        this.infiniteScroll.complete();
        if (!res.data.hasNextPage) {
          this.infiniteScroll.enable(false);
        }
        this.circleList = [...this.circleList, ...res.data.list];
      }
    });
  }

  ngOnInit() {
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

}
